<?php
require_once(dirname(__FILE__)."/../include/db_mysql.php");

function get_productlist_by_tagId($args)
{
	$sql = new SqlClass;
	//连接数据库
	$sql->connect();
	//执行sql语句	
	$tagArray= spliti(',',$args['tag']);
	$productArray = array();
	$productIdArray = array();
	//控制是否是第一次循环
	$flag = true;
	foreach($tagArray as $tag)
	{
		if(empty($tag))
		{
			continue;
		}
       $cmd = "select dg_productId from producttag where tagId= '$tag' ";    
       $rs=$sql->exec_sql($cmd);
	   $tempArray = array();
       while($row = mysql_fetch_array($rs))
		{
		   if($flag)
			{
             array_push($productIdArray,$row['dg_productId']);
			}
			else
			{
			 array_push($tempArray,$row['dg_productId']);
			}
	    }
		$flag = false;
		if(!empty($tempArray))
		{
			foreach($productIdArray as $temp)
			{
				if(!in_array($temp,$tempArray))		
				{
                    $founded = array_search($temp,$productIdArray);
					array_splice($productIdArray,$founded,1);
				}           
			}
		}
	}
    foreach($productIdArray as $productId)
	{
		$temp = get_productioninfo_by_id($productId,$sql);
		array_push($productArray,$temp);
	}

	 $totalPageNum = ceil(count($productArray)/30);
	 $resultByPage = array_splice($productArray,($args['page']-1)*30,30);
     $return = array($resultByPage,$totalPageNum);  
	$sql->close();
	return json_encode($return);
}
function get_productioninfo_by_id($id,$link)
{
    $productArray = array();
    $cmd = "select productName,mainPic,price,description from dg_product where productId = '$id'";    
    $rs=$link->exec_sql($cmd);
	while($row = mysql_fetch_array($rs))
	{
		$productArray['main_pic']= $row['mainPic'];
		$productArray['productName']= $row['productName'];
		$productArray['price']= $row['price'];
	    $productArray['description']= $row['description'];
	}
	return $productArray;
}
?>

